﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using Model;
using System.Data;
namespace Manager
{
    /// <summary>
    /// HW_SchoolMergeLog业务逻辑
    /// 创建人：Wilson
    /// 创建时间：2013-1-29  
    /// </summary>
    public class HW_SchoolMergeLogManager
    {
        			
        #region##通过主键ID得到HW_SchoolMergeLog
        /// <summary>
        /// 功能：通过ID得到HW_SchoolMergeLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_SchoolMergeLog GetById(int mergelodid)
        {            
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
			{
				return ws.GetById(mergelodid);
			}
        }
        public IList<HW_SchoolMergeLog> GetByNewSchoolId(string schoolId)
        {
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
            {
                return ws.GetByNewSchoolId(schoolId);
            }
        }
        public DataTable GetMergeSchool(string schoolcode)
        {
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
            {
                return ws.GetMergeSchool(schoolcode);
            }
        }
        #endregion
		
		#region##通过主键ID删除HW_SchoolMergeLog
        /// <summary>
        /// 功能：通过主键ID删除HW_SchoolMergeLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(int mergelodid)
		{
			using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
			{
				return ws.DeleteById(mergelodid);
			}
		}
		#endregion			
		
        #region##添加HW_SchoolMergeLog
        /// <summary>
        /// 功能：添加HW_SchoolMergeLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_SchoolMergeLog hw_schoolmergelog)
        {
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
			{
				return ws.Add(hw_schoolmergelog);
			}
        }
        public int Add(HW_SchoolMergeLog hw_schoolmergelog,System.Data.SqlClient.SqlTransaction trans)
        {
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
            {
                return ws.Add(trans,hw_schoolmergelog);
            }
        }
        public void SchoolMerge(HW_School newSchool, string[] oldshools,bool addNewUser,bool permit)
        {
            HW_DistrictManager dismanager = new HW_DistrictManager();
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(Utility.WebConn.connString);
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            System.Data.SqlClient.SqlTransaction trans = conn.BeginTransaction();
            try
            {
                HW_SchoolManager manager = new HW_SchoolManager();
                newSchool.Schools = new List<HW_School>();
                foreach (var m in oldshools)
                {
                    HW_SchoolMergeLog log = new HW_SchoolMergeLog();
                    log.YearCode = DateTime.Now.Year.ToString();
                    var oldSchool = manager.GetById(m);
                    if (oldSchool.Statute != "T")
                        throw new Exception(oldSchool.Name + "已经停办，或者学校已经合并过");
                    log.SchoolCode = m;
                    log.SchoolName = oldSchool.Name;
                    log.NewSchoolCode = newSchool.SchoollCode;
                    log.NewSchoolName = newSchool.Name;
                    log.MergeDate = DateTime.Now.Date;
                    Add(log, trans);
                    manager.ChangeState(m, "F", trans);
                    newSchool.Schools.Add(oldSchool);
                }
                newSchool.Statute = "T";
                manager.Add(newSchool,permit,addNewUser,trans);
                trans.Commit();
            }
            catch(Exception e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion
		
		#region##得到HW_SchoolMergeLog集合
		/// <summary>
        /// 功能：得到HW_SchoolMergeLog集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_SchoolMergeLog> GetList()
		{
		    using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
			{
				return ws.GetList();
			}
		}
		#endregion
		
		#region##修改HW_SchoolMergeLog
        /// <summary>
        /// 功能：修改HW_SchoolMergeLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_SchoolMergeLog(HW_SchoolMergeLog hw_schoolmergelog)
        {
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
			{
				return ws.ModHW_SchoolMergeLog(hw_schoolmergelog);
			}
        }
        #endregion
		
		#region##统计HW_SchoolMergeLog
		/// <summary>
        /// 修改HW_SchoolMergeLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
			{
				return ws.SelectCount();
			}
		}
        /// <summary>
        /// 修改HW_SchoolMergeLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
			{
				return ws.SelectCountById();
			}
        }
		#endregion
        #region##自定义HW_SchoolMergeLog
        public DataTable Search(int pageIndex, int pageSize, out int count, string SearchString)
        {

            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
            {
                return ws.Search(pageIndex, pageSize, out count, SearchString);
            }
        }
        public DataTable GetList(int pageIndex, int pageSize, out int count)
        {

            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
            {
                return ws.GetList(pageIndex, pageSize, out count);
            }
        }
        public void DeMerge(string schoolcode)
        {
            HW_SchoolService schoolService = new HW_SchoolService();
            HW_School_UserService schooluserService = new HW_School_UserService();
            using (HW_SchoolMergeLogService ws = new HW_SchoolMergeLogService())
            {
                var school = ws.GetByOldSchoolId(schoolcode).ToList();
                if (school.Count()>0)
                    throw new Exception("该学校已经再次被" + school[0].NewSchoolName+ "合并过，已过期不能恢复合并");
                var oldschools = ws.GetByNewSchoolId(schoolcode);

                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(Utility.WebConn.connString);
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                System.Data.SqlClient.SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    schooluserService.DeleteBySchoolCode(trans, schoolcode);
                    ws.DeleteByNewSchool(trans, schoolcode);//删除合并记录
                    schoolService.DeleteById(trans, schoolcode);//删除新学校
                    for (int i = 0; i < oldschools.Count; i++)
                    {
                        schoolService.ChangeState(oldschools[i].SchoolCode, "T", trans);
                    }
                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
                
            }
        }
        #endregion
    }
}